using System;
using System.Collections.Generic;
using System.Text;

namespace Util.Extensions
{
    /// <summary>
    /// 创建人：宙冰
    /// 日  期：2020.04.04
    /// 描  述：字符串扩展类
    /// </summary>
    public static class StringExtensions
    {
        /// <summary>
        /// 验证字符串是否为空，如果为空或null，则抛出空字符串异常
        /// </summary>
        /// <param name="str">要验证的字符串</param>
        /// <param name="error_message">错误说明</param>
        /// <param name="error_fix">错误说明的后缀，默认为“为空”</param>
        public static void ValidEmptyThrowException(this string str, string error_message, string error_fix = "为空")
        {
            if (string.IsNullOrEmpty(str))
            {
                throw new Exception($"{error_message}{error_fix}");
            }
        }

        /// <summary>
        /// 验证字符串是否为空，如果为空或null，则抛出空字符串异常，否则返回原值
        /// </summary>
        /// <param name="str">要验证的字符串</param>
        /// <param name="error_message">错误说明</param>
        /// <param name="error_fix">错误说明的后缀，默认为“为空”</param>
        /// <returns></returns>
        public static string ValidEmptyThrowExceptionOrReturn(this string str, string error_message, string error_fix = "为空")
        {
            str.ValidEmptyThrowException(error_message, error_fix);
            return str;
        }

        /// <summary>
        /// 验证字符串是否为侬，如果为空或null，则返回 default_value
        /// </summary>
        /// <param name="str">要验证的字符串</param>
        /// <param name="default_value">在str为空或null返回的值</param>
        /// <returns></returns>
        public static string ValidEmptyReturn(this string str, string default_value = "")
        {
            if (string.IsNullOrEmpty(str))
            {
                return default_value;
            }
            else
            {
                return str;
            }
        }

        /// <summary>
        /// 把日期字符转换成日期
        /// 日期字符格式：yyyy/MM/dd HH:mm:ss，yyyy-MM-dd HH:mm:ss
        /// </summary>
        /// <param name="str">要转换成日期的字符串</param>
        /// <returns></returns>
        public static DateTime? ToDate(this string str)
        {
            if (str == null)
            {
                return null;
            }

            DateTime dt;
            if (DateTime.TryParse(str, out dt))
            {
                return dt;
            }

            return null;
        }

        /// <summary>
        /// 把日期字符转换成日期
        /// 日期字符格式：yyyyMMddHHmmss
        /// </summary>
        /// <param name="str">要转换成日期的字符串</param>
        /// <param name="type">
        /// 1:yyyyMMddHHmmss
        /// 2:
        /// </param>
        /// <returns></returns>
        public static DateTime? ToDate(this string str, int type)
        {
            DateTime? dt = null;
            switch (type)
            {
                case 1:
                    {
                        int year = Convert.ToInt32(str.Substring(0, 4));
                        int month = Convert.ToInt32(str.Substring(4, 2));
                        int day = Convert.ToInt32(str.Substring(6, 2));
                        int hour = 0;
                        int minute = 0;
                        int second = 0;
                        if (str.Length >= 10)
                        {
                            hour = Convert.ToInt32(str.Substring(8, 2));
                        }
                        if (str.Length >= 12)
                        {
                            minute = Convert.ToInt32(str.Substring(10, 2));
                        }
                        if (str.Length == 14)
                        {
                            second = Convert.ToInt32(str.Substring(12, 2));
                        }
                        dt = new DateTime(year, month, day, hour, minute, second);
                    }
                    break;
                default:
                    break;
            }
            return dt;
        }

        /// <summary>
        /// 對字串進行URL編碼
        /// </summary>
        /// <param name="str">要進行編碼的字串</param>
        /// <param name="isUrl">是否放到URL后面</param>
        /// <returns></returns>
        public static string UrlPathEncode(this string str)
        {

            return System.Web.HttpUtility.UrlEncode(str);

        }

        /// <summary>
        /// 對字串進行URL解碼
        /// </summary>
        /// <param name="str">要進行解碼的字串</param>
        /// <param name="isUrl">是否放到URL后碼</param>
        /// <returns></returns>
        public static string UrlPathDecode(this string str)
        {
            return System.Web.HttpUtility.UrlDecode(str);
        }

        /// <summary>
        /// 把字串封裝成成Json字串
        /// </summary>
        /// <param name="str">要封裝成Json的字串</param>
        /// <param name="success">是否成功</param>
        /// <param name="isAddQuot">是否給字串自動加雙引號</param>
        /// <param name="isBoolQuot">布爾值是否加引號</param>
        /// <returns></returns>
        public static string ToJson(this string str, bool success, bool isAddQuot = true, bool isBoolQuot = false, bool isEncode = true)
        {
            string strQuot = "\"";
            if (!isAddQuot)
            {
                strQuot = "";
            }
            else
            {
                str = str.JsonTranscoding();
            }

            string strSuccess = success.ToString().ToLower();
            if (isBoolQuot)
            {
                strSuccess = "\"" + strSuccess + "\"";
            }

            return "{\"success\":" + strSuccess + ",\"msg\":" + strQuot + (isEncode ? str.UrlPathEncode() : str) + strQuot + "}";
        }

        /// <summary>
        /// 進行Json轉碼
        /// </summary>
        /// <param name="str">要進行轉碼的字串</param>
        /// <returns></returns>
        public static string JsonTranscoding(this string str)
        {
            str = str.Replace("\"", "“");
            str = str.Replace("\'", "’");
            str = str.Replace("\r\n", "");
            str = str.Replace("\r", "");
            str = str.Replace("\n", "");
            str = str.Replace("\\", "/");
            str = str.Replace("&", "＆");
            return str;
        }

        /// <summary>
        /// 清空字串开始的字串，如果没有，则返回原字串
        /// </summary>
        /// <param name="str">原字串</param>
        /// <param name="trim_str">要清除的字串</param>
        /// <returns></returns>
        public static string TrimStartString(this string str, string trim_str)
        {
            if (str.IndexOf(trim_str) == 0)
            {
                return str.Substring(trim_str.Length);
            }
            else
            {
                return str;
            }
        }

        /// <summary>
        /// 清空字串结尾的字串，如果没有，则返回原字串
        /// </summary>
        /// <param name="str">原字串</param>
        /// <param name="trim_str">要清除的字串</param>
        /// <returns></returns>
        public static string TrimEndString(this string str, string trim_str)
        {
            if (str.LastIndexOf(trim_str) == str.Length - trim_str.Length)
            {
                return str.Substring(0, str.Length - trim_str.Length);
            }
            else
            {
                return str;
            }
        }

        public static T ToEnum<T>(this string str) where T : new()
        {
            T color = (T)Enum.Parse(typeof(T), str);
            return color;
        }
    }
}
